<?php 
require_once 'SqlHelper.php';
require_once 'Emp.class.php';
class Empservice{
    public function getPageCount($pageSize){
        $sql="select count(id) from emp";
        $sqlhelper=new SqlHelper();
        $res=$sqlhelper->execute_dql($sql);
        if($row=$res->fetch_row()){
            $pageCout=ceil($row[0]/$pageSize);
            mysqli_free_result($res);
            $sqlhelper->close_sql();
            return $pageCout;
            
        }
        return false;
    }
    public function getData($pageNow,$pageSize){
        $sql="select * from emp limit ".($pageNow-1)*$pageSize.",$pageSize";
        $sqlhelper=new SqlHelper();
        $res=$sqlhelper->execute_dql2($sql);
        return $res;
    }
    public function getFenyePage($fenyePage){
        $sqlHelper=new SqlHelper();
        $sql1="select * from emp limit ".($fenyePage->pageNow-1)*$fenyePage->pageSize.",".$fenyePage->pageSize;
        $sql2="select count(id) from emp";
     
        $sqlHelper->execute_dql_fenye($sql1, $sql2, $fenyePage);
        $sqlHelper->close_sql();
    }
    public function delEmpById($id){
        $sql="delete from emp where id=$id";
        $sqlHelper=new SqlHelper();
        return $sqlHelper->execute_dml($sql);
    }
    public function addEmp($name,$grade,$email,$salary){
        $sql="insert into emp(name,grade,email,salary) values('$name','$grade','$email','$salary')";
     
        $sqlHelper=new SqlHelper();
        $res=$sqlHelper->execute_dml($sql);
        $sqlHelper->close_sql();
        return $res;
    }
    public function getEmpById($id){
        $sql="select * from emp where id=$id";
        $sqlHelper=new SqlHelper();
        $arr=$sqlHelper->execute_dql2($sql);
        $emp=new Emp();
        $emp->setId($arr[0]['id']);
        $emp->setEmail($arr[0]['email']);
        $emp->setGrade($arr[0]['grade']);
        $emp->setName($arr[0]['name']);
        $emp->setSalary($arr[0]['salary']);
        return $emp;
       # return $arr;
    }
    public function updateEmp($id,$name,$grade,$email,$salary){
        $sql="update emp set name='$name',grade='$grade',email='$email',salary='$salary' where id=$id";
     
        $sqlHelper=new SqlHelper();
        $res=$sqlHelper->execute_dml($sql);
        $sqlHelper->close_sql();
        return $res;
    }
}
?>